package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Peachblossom;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Date;

/**
 * @author mwj
 * @date 2022/1/7
 * @TIME:23:48
 */
@DubboService
public class PeachblossomApiImpl implements PeachblossomApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //随机查询一个语音消息返回
    @Override
    public Peachblossom findRandom(Long userId) {

        //1、创建统计对象，设置统计参数
        Criteria criteria = Criteria.where("receiveId").is(null);
        TypedAggregation aggregation = Aggregation.newAggregation(Peachblossom.class, Aggregation.sample(1), Aggregation.match(criteria));
        //2、调用mongoTemplate方法统计
        AggregationResults<Peachblossom> results = mongoTemplate.aggregate(aggregation, Peachblossom.class);
        //3、获取统计结果
        List<Peachblossom> list = results.getMappedResults();

        return list.get(0);
    }

    //更新
    @Override
    public void update(Peachblossom peachblossom) {
        Query query = Query.query(Criteria.where("pid").is(peachblossom.getPid()));
        Update update = Update.update("receiveId", peachblossom.getReceiveId());
        mongoTemplate.updateFirst(query, update, Peachblossom.class);
    }

    //保存
    @Override
    public void save(Peachblossom peachblossom) {
        mongoTemplate.save(peachblossom);
    }


    @Override
    public Boolean sendSoundFile(Long userId, String soundUrl) {
        Peachblossom pb = new Peachblossom();
        pb.setUserId(userId);
        pb.setSoundUrl(soundUrl);
        // todo 数字解析异常
        //pb.setCreated(Long.valueOf(new SimpleDateFormat("yyyy-MM-dd").format(new Date())));
        pb.setCreated(System.currentTimeMillis());
        mongoTemplate.insert(pb);
        Criteria criteria = Criteria.where("soundUrl").is(null).and("userId").is(userId);
        Query query = Query.query(criteria);
        Peachblossom one = mongoTemplate.findOne(query, Peachblossom.class);
        if (!StringUtils.isEmpty(one)) {
            return true;
        }
        return false;
    }

    @Override
    public Integer selectSendCountInToday(Long userId) {
        Criteria criteria = Criteria.where("created").is(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                .and("userId").is(userId);
        Query query = Query.query(criteria);
        Integer count = Math.toIntExact(mongoTemplate.count(query, Peachblossom.class));
        return count;
    }
}
